package rmy.service.impl;


import rmy.device.onbon.*;
import rmy.domain.VoicCmd;
import rmy.domain.VoicParam;
import rmy.service.VoicInterface;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

public class OnbonVoicDevice implements VoicInterface {


    @Override
    public boolean sendVoic(String strIp, int nPort, VoicParam compParam, VoicCmd voicCmd) {
        // 创建一个区域 1
        byte id = 0x00;
        short x = 0;
        short y = 0;
        short w = 64;
        short h = 16;
        // 添加到列表
        List<BxArea> areas = new ArrayList<BxArea>();
        String hello = voicCmd.getSound_content();
        try {
            String s2 = "";
            byte[] data2 = s2.getBytes("gb2312");
            //五代K卡最后一个参数给true、非5代的给false
            BxAreaDynamic area2 = new BxAreaDynamic(id, x, y, w, h, data2, true);
            byte[] soundData = hello.getBytes("gb2312");
            //
            // 语音模式
            area2.setSoundMode((byte) 0x02);
            // 人声模式
            area2.setSoundPerson((byte) voicCmd.getSound_person());
            // 重复次数
            area2.setSoundRepeat((byte) voicCmd.getSound_repeat());
            // 发音速度
            area2.setSoundSpeed((byte) voicCmd.getSound_speed());
            // 音量
            area2.setSoundVolume((byte) voicCmd.getSound_volume());
            // 要发声的文字，仅在 soundMode = 0x02时有效
            area2.setSoundData(soundData);
            areas.add(area2);

            // 创建一个发送动态区命令
            BxCmd cmd = new BxCmdSendDynamicArea(areas);

            // 对命令进行封装
            BxDataPack dataPack = new BxDataPack(cmd);

            // 生成命令序列
            byte[] seq = dataPack.pack();
            // 创建 Socket
            Socket client = new Socket();
            // 创建 socket 地址
            SocketAddress address = new InetSocketAddress(strIp, nPort);

            try {
                // 建立 TCP 连接
                client.connect(address, 3000);
                // 设置读超时时间
                client.setSoTimeout(3000);
                // 创建输出流
                OutputStream out = client.getOutputStream();
                // 创建输入流
                InputStream in = client.getInputStream();
                // 写入数据
                out.write(seq);
                // 回读返回帧
                byte[] resp = new byte[1024];
                int len = in.read(resp);
                // parse
                BxResp bxResp = BxResp.parse(resp, len);
                boolean bRet = false;
                // 判断命令是否被正确执行
                if(bxResp.isAck()) {
                    System.out.println("Well done!!");
                    bRet = true;
                }
                else {
                    System.out.println("I am sorry!");
                }
                //
                out.close();
                in.close();
                client.close();
                return bRet;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }

        return false;
    }
}
